//
//  HYHomeViewModel.m
//  XPlayer
//
//  Created by liguoqing on 2023/8/12.
//

#import "HYHomeViewModel.h"
#import <BaseWithRAC/BaseModel+LKDBHelper_DLExtension.h>
#import <HYVideoPlayer/HYVideoPlayerConfig.h>
#import <HYVideoPlayer/HYVideoModel.h>
#import "HYHomeFolderModel.h"
#import "HYHomeVideoModel.h"
#import "HYPreviewHistoryManager.h"

@interface HYHomeViewModel ()

@property (nonatomic, strong) NSMutableArray *headerSource;
/// 全部数据源
@property (nonatomic, strong) NSMutableArray *dataSource;
/// 视频数据源
@property (nonatomic, strong) NSMutableArray *videoSource;
/// 音频数据源
@property (nonatomic, strong) NSMutableArray *audioSource;
/// 文件夹
@property (nonatomic, strong) NSMutableArray *folderSoucre;
/// 视频数据
@property (nonatomic, strong) NSMutableArray *videoPlaySource;
/// 音频数据
@property (nonatomic, strong) NSMutableArray *audioPlaySource;

@end

@implementation HYHomeViewModel

- (instancetype)initWithFolderLocation:(NSString *)folderLocation
                  folderIdentification:(NSString *)folderIdentification
{
    self = [super init];
    if(self){
        self.folderLocation = folderLocation;
        self.folderIdentification = folderIdentification;
        self.isHome = folderLocation.length?NO:YES;
        [self initSignal];
    }
    return self;
}

- (instancetype)initWithParentPath:(NSString *)parentPath
                 currentFolderPath:(NSString *)currentFolderPath
{
    self = [super init];
    if(self){
        self.parentPath = parentPath;
        self.folderPath = currentFolderPath;
        [self initSignal];
    }
    return self;
}

- (void)initSignal
{
    self.setSubject = [RACSubject subject];
    self.reloadSubject = [RACSubject subject];
    self.showFileSubject = [RACSubject subject];
    self.popSubject = [RACSubject subject];
    self.refreshSubject = [RACSubject subject];
}

- (void)fetchData:(HYHomeFileType)type
{
    [self.headerSource removeAllObjects];
    [self.dataSource removeAllObjects];
    [self.videoSource removeAllObjects];
    [self.audioSource removeAllObjects];
    [self.folderSoucre removeAllObjects];
    [self.videoPlaySource removeAllObjects];
    [self.audioPlaySource removeAllObjects];
    if(self.isHome){
        [self addDefaultFolderSoucre];
    }
    [self addCreateFolderSoucre];
    [self addCreateVideoSource];
    [self.updatedContentSignal sendNext:self.dataSource];
}

- (void)addDefaultFolderSoucre
{
    NSArray *folderSource = [HYHomeFolderModel searchWithSQL:[NSString stringWithFormat:@"select * from FOLDERLIST where folderLocation = '%@'",self.folderIdentification]];
    if(!folderSource.count){
        HYHomeFolderModel *documentModel = [[HYHomeFolderModel alloc]init];
        documentModel.folderType = HYHomeFolderType_Document;
        documentModel.folderLocation = self.folderIdentification;
        documentModel.folderIdentification = @"fileImport-vplayer";
        documentModel.folderName = NSLocalizedString(@"文件导入", nil);
        [documentModel saveToDB];
        
        HYHomeFolderModel *photoModel = [[HYHomeFolderModel alloc]init];
        photoModel.folderType = HYHomeFolderType_Photo;
        photoModel.folderLocation = self.folderIdentification;
        photoModel.folderIdentification = @"myPhoto-vplayer";
        photoModel.folderName = NSLocalizedString(@"我的相册", nil);
        [photoModel saveToDB];
        
        HYHomeFolderModel *downloadModel = [[HYHomeFolderModel alloc]init];
        downloadModel.folderType = HYHomeFolderType_Cloud;
        downloadModel.folderLocation = self.folderIdentification;
        downloadModel.folderIdentification = @"baidu-vplayer";
        downloadModel.folderName = NSLocalizedString(@"百度云盘", nil);
        [downloadModel saveToDB];
        
        HYHomeFolderModel *collectModel = [[HYHomeFolderModel alloc]init];
        collectModel.folderType = HYHomeFolderType_Collect;
        collectModel.folderLocation = self.folderIdentification;
        collectModel.folderIdentification = @"myCollect-vplayer";
        collectModel.folderName = NSLocalizedString(@"我的收藏", nil);
        [collectModel saveToDB];
    }
}

- (void)addCreateFolderSoucre
{
    NSArray *folderSource = [HYHomeFolderModel searchWithSQL:[NSString stringWithFormat:@"select * from FOLDERLIST where folderLocation = '%@'",self.folderIdentification]];
    if(folderSource.count){
        [folderSource enumerateObjectsUsingBlock:^(HYHomeFolderModel *folderModel, NSUInteger idx, BOOL * _Nonnull stop) {
            // 防止系统切换语言后，文件语言没有切换
            if(folderModel.folderType == HYHomeFolderType_Document){
                folderModel.folderName = NSLocalizedString(@"文件导入", nil);
                [folderModel updateToDB];
            }else if (folderModel.folderType == HYHomeFolderType_Photo){
                folderModel.folderName = NSLocalizedString(@"我的相册", nil);
                [folderModel updateToDB];
            }else if (folderModel.folderType == HYHomeFolderType_Cloud){
                folderModel.folderName = NSLocalizedString(@"百度云盘", nil);
                [folderModel updateToDB];
            }else if (folderModel.folderType == HYHomeFolderType_Collect){
                folderModel.folderName = NSLocalizedString(@"我的收藏", nil);
                [folderModel updateToDB];
            }
            HYHomeFileItemViewModel *fileViewModel = [[HYHomeFileItemViewModel alloc]initWitFolderModel:folderModel];
            fileViewModel.setSubject = self.setSubject;
            if(fileViewModel.fileType == HYHomeFileType_Document){
                [self.headerSource insertObject:fileViewModel atIndex:0];
            }else if (fileViewModel.fileType == HYHomeFileType_Photo){
                [self.headerSource insertObject:fileViewModel atIndex:1];
            }else if (fileViewModel.fileType == HYHomeFileType_Cloud){
                [self.headerSource insertObject:fileViewModel atIndex:2];
            }else if (fileViewModel.fileType == HYHomeFileType_Collect){
                [self.headerSource insertObject:fileViewModel atIndex:3];
            }else{
                [self.folderSoucre insertObject:folderModel atIndex:0];
                [self.dataSource insertObject:fileViewModel atIndex:0];
            }
        }];
    }
}

- (void)addCreateVideoSource
{
    NSArray *videoSource = [HYHomeVideoModel searchWithSQL:[NSString stringWithFormat:@"select * from VIDEOLIST where folderIdentification = '%@'",self.folderIdentification]];
    if(videoSource.count){
        [videoSource enumerateObjectsUsingBlock:^(HYHomeVideoModel *videoModel, NSUInteger idx, BOOL * _Nonnull stop) {
            HYHomeFileItemViewModel *fileModel = [[HYHomeFileItemViewModel alloc]initWitVideoModel:videoModel];
            fileModel.setSubject = self.setSubject;
            if(idx==0){
                [self.dataSource addObject:fileModel];
            }else{
                [self.dataSource insertObject:fileModel atIndex:self.folderSoucre.count];
            }
            if(fileModel.fileType == HYHomeFileType_Video){
                [self.videoSource insertObject:fileModel atIndex:0];
            }else{
                [self.audioSource insertObject:fileModel atIndex:0];
            }
        }];
        [self dealPlayDataWithVideoSource:videoSource];
    }
}

- (void)dealPlayDataWithVideoSource:(NSArray *)videoSource
{
    [videoSource enumerateObjectsUsingBlock:^(HYHomeVideoModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
        HYVideoModel *videoModel = [[HYVideoModel alloc]init];
        videoModel.videoTitle = model.videoTitle;
        if(model.isAudio){
            videoModel.videoUrl = model.videoUrl;
            videoModel.isAudio = YES;
            videoModel.isPrivate = model.isLock;
            videoModel.createDate = model.createDate;
            videoModel.importDate = model.importDate;
            videoModel.videoSize = model.videoSize;
            videoModel.videoDuration = model.videoDuration;
            [self.audioPlaySource insertObject:videoModel atIndex:0];
        }else{
            if(model.isLocal){
                videoModel.videoUrl = [HYFileManager fetchPhotoVideoImportPathWithVideoName:model.videoTitle];
            }else{
                videoModel.videoUrl = model.videoUrl;
            }
            videoModel.videoImage = [UIImage imageNamed:[HYFileManager videoImageSavePathWithFileName:model.imageName]];
            videoModel.videoImagePath = model.imageName;
            videoModel.isPrivate = model.isLock;
            videoModel.createDate = model.createDate;
            videoModel.importDate = model.importDate;
            videoModel.videoSize = model.videoSize;
            videoModel.videoDuration = model.videoDuration;
            [self.videoPlaySource insertObject:videoModel atIndex:0];
        }
    }];
}

- (NSInteger)numberOfRowsIsHeader:(BOOL)isHeader fileType:(HYHomeFileType)fileType
{
    if(isHeader){
        return self.headerSource.count;
    }else{
        if(fileType == HYHomeFileType_Video){
            return self.videoSource.count;
        }else if (fileType == HYHomeFileType_Audio){
            return self.audioSource.count;
        }
        return self.dataSource.count;
    }
}

- (id)cellForRowAtIndexPath:(NSIndexPath *)indexPath isHeader:(BOOL)isHeader fileType:(HYHomeFileType)fileType
{
    if(isHeader){
        return self.headerSource[indexPath.item];
    }else{
        if(fileType == HYHomeFileType_Video){
            return self.videoSource[indexPath.row];
        }else if (fileType == HYHomeFileType_Audio){
            return self.audioSource[indexPath.row];
        }
        return self.dataSource[indexPath.row];
    }
}

- (HYHomeFileType)fetchFileType:(NSIndexPath *)indexPath fileType:(HYHomeFileType)fileType
{
    HYHomeFileItemViewModel *fileModel;
    if(fileType == HYHomeFileType_Video){
        fileModel = self.videoSource[indexPath.row];
    }else if (fileType == HYHomeFileType_Audio){
        fileModel = self.audioSource[indexPath.row];
    }else{
        fileModel = self.dataSource[indexPath.row];
    }
    return fileModel.fileType;
}

- (void)deleteRowsAtIndexPaths:(NSIndexPath *)indexPath fileType:(HYHomeFileType)fileType
{
    HYHomeFileItemViewModel *fileModel = [self cellForRowAtIndexPath:indexPath isHeader:NO fileType:fileType];
    if(fileModel.fileType == HYHomeFileType_Video){
        [self.videoSource removeObject:fileModel];
    }else if (fileModel.fileType == HYHomeFileType_Video){
        [self.audioSource removeObject:fileModel];
    }
    [fileModel deleteToDB];
    [self.dataSource removeObject:fileModel];
}

- (void)didSelectRowAtIndexPath:(NSIndexPath *)indexPath isHeader:(BOOL)isHeader fileType:(HYHomeFileType)fileType
{
    HYHomeFileItemViewModel *itemViewModel = [self cellForRowAtIndexPath:indexPath isHeader:isHeader fileType:fileType];
    [self.showFileSubject sendNext:itemViewModel];
}

- (HYVideoViewModel *)fetchPlayVideoConfigWithItemViewModel:(HYHomeFileItemViewModel *)itemViewModel
{
    //1.如果点击的是非私密视频，需要把私密视频过滤掉
    HYVideoModel *currentModel;
    if(itemViewModel.fileType == HYHomeFileType_Video){
        currentModel = [self fetchCurrentVideoWithFileModel:itemViewModel];
    }else{
        currentModel = [self fetchCurrentAudioWithFileModel:itemViewModel];
    }
    if(currentModel){
        currentModel.isSelect = YES;
        HYVideoPlayerConfig *config = [[HYVideoPlayerConfig alloc]init];
        config.isBasePlayer = YES;
        config.currentVideoModel = currentModel;
        config.videoSoucres = [self fetchPlaySourceWithItemViewModel:itemViewModel];
        config.isAudioMode = itemViewModel.fileType == HYHomeFileType_Video?NO:YES;
        HYVideoViewModel *viewModel = [[HYVideoViewModel alloc]initWithFolderIdentification:self.folderIdentification
                                                                                  isPrivate:self.isPrivate
                                                                                videoConfig:config];
        return viewModel;
    }
    return nil;
}

- (HYHomeFileItemViewModel *)fetchMyDocumentViewModel
{
    return self.dataSource[0];
}

#pragma mark - 获取当前视频
- (HYVideoModel *)fetchCurrentVideoWithFileModel:(HYHomeFileItemViewModel *)fileModel
{
    __block HYVideoModel *playModel;
    [self.videoPlaySource enumerateObjectsUsingBlock:^(HYVideoModel *videoModel, NSUInteger idx, BOOL * _Nonnull stop) {
        if([videoModel.videoUrl isEqualToString:fileModel.filePath]){
            playModel = videoModel;
            *stop = YES;
        }
    }];
    return playModel;
}

#pragma mark - 获取点击音频的索引
- (HYVideoModel *)fetchCurrentAudioWithFileModel:(HYHomeFileItemViewModel *)fileModel
{
    __block HYVideoModel *playModel;
    [self.audioPlaySource enumerateObjectsUsingBlock:^(HYVideoModel *videoModel, NSUInteger idx, BOOL * _Nonnull stop) {
        if([videoModel.videoUrl isEqualToString:fileModel.filePath]){
            playModel = videoModel;
            *stop = YES;
        }
    }];
    return playModel;
}

- (NSArray *)fetchPlaySourceWithItemViewModel:(HYHomeFileItemViewModel *)itemViewModel
{
    if(itemViewModel.isLock){
        if(itemViewModel.fileType == HYHomeFileType_Video){
            return self.videoPlaySource;
        }
        return self.audioPlaySource;
    }
    __block NSMutableArray *playSource = [NSMutableArray array];
    if(itemViewModel.fileType == HYHomeFileType_Video){
        [self.videoPlaySource enumerateObjectsUsingBlock:^(HYVideoModel *videoModel, NSUInteger idx, BOOL * _Nonnull stop) {
            if(!videoModel.isPrivate){
                [playSource addObject:videoModel];
            }
        }];
    }else{
        [self.audioPlaySource enumerateObjectsUsingBlock:^(HYVideoModel *audioModel, NSUInteger idx, BOOL * _Nonnull stop) {
            if(!audioModel.isPrivate){
                [playSource addObject:audioModel];
            }
        }];
    }
    return [playSource copy];
}

#pragma mark - 新建文件夹
- (void)addNewFolderWithFolderName:(NSString *)folderName completeBlock:(HYBIdBlock)completeBlock
{
    NSString *sqlStr = [NSString stringWithFormat:@"SELECT *from FOLDERLIST WHERE folderLocation = '%@' AND folderName = '%@'",self.folderIdentification,folderName];
    NSArray *result = [HYHomeFolderModel searchWithSQL:sqlStr];
    if(!result.count){
        HYHomeFolderModel *folderModel = [[HYHomeFolderModel alloc]init];
        folderModel.folderType = HYHomeFolderType_Folder;
        folderModel.folderLocation = self.folderIdentification;
        folderModel.folderIdentification = [HYFileManager folderIdentification];
        folderModel.folderName = folderName;
        folderModel.createDate = [CommUtls encodeTimeYearMonthDay:[NSDate date]];
        folderModel.isLock = NO;
        [folderModel saveToDB];
        // 从数据库中重新获取一次，是为了_id有值，防止updateToDB更新不成功
        NSString *sqlStr = [NSString stringWithFormat:@"SELECT *from FOLDERLIST WHERE folderLocation = '%@' AND folderName = '%@'",folderModel.folderLocation,folderModel.folderName];
        HYHomeFolderModel *sqFolderModel = [[HYHomeFolderModel searchWithSQL:sqlStr] firstObject];
        HYHomeFileItemViewModel *fileModel = [[HYHomeFileItemViewModel alloc]initWitFolderModel:sqFolderModel];
        fileModel.setSubject = self.setSubject;
        if(completeBlock){
            completeBlock(fileModel);
        }
        [self.folderSoucre insertObject:folderModel atIndex:0];
        [self.dataSource insertObject:fileModel atIndex:0];
        [self.updatedContentSignal sendNext:self.dataSource];
    }else{
        [HYHub showMessage:NSLocalizedString(@"文件夹已存在", nil)];
    }
}


- (void)createFolderWithFolderName:(NSString *)folderName
                     completeBlock:(HYBIdBlock)completeBlock
{
    
}

#pragma mark - 获取当前页面的文件夹
- (NSArray *)fetchCurrentListFolder:(HYHomeFileItemViewModel *)fileVM
{
    __block NSMutableArray *folderSource = [NSMutableArray array];
    if(self.folderLocation.length){
        HYHomeFileItemViewModel *parentVM = [[HYHomeFileItemViewModel alloc]init];
        parentVM.fileName = NSLocalizedString(@"上一级", nil);
        parentVM.folderIdentification = self.folderLocation;
        parentVM.fileType = HYHomeFileType_Folder;
        [folderSource addObject:parentVM];
    }
    [self.dataSource enumerateObjectsUsingBlock:^(HYHomeFileItemViewModel *itemViewModel, NSUInteger idx, BOOL * _Nonnull stop) {
        if(itemViewModel.fileType == HYHomeFileType_Folder && ![fileVM isEqual:itemViewModel]){
            [folderSource addObject:itemViewModel];
        }
    }];
    return [folderSource copy];
}

#pragma mark - 文件夹重命名
- (void)modifyFileNameWithFileViewModel:(HYHomeFileItemViewModel *)fileVM
{
    NSInteger index = [self.dataSource indexOfObject:fileVM];
    HYLayoutType type =  [HYMineSetManager shareManager].layoutType;
    if(fileVM.fileType == HYHomeFileType_Video || fileVM.fileType == HYHomeFileType_Audio){
        HYVideoModel *currentModel = [self fetchCurrentVideoWithFileModel:fileVM];
        if([fileVM modifyFileNameToDB]){
            if(currentModel){
                currentModel.videoTitle = fileVM.videoModel.videoTitle;
                currentModel.videoUrl = fileVM.filePath;
            }
        }else{
            [HYHub showMessage:NSLocalizedString(@"重命名失败", nil)];
        }
        if(type == HYLayoutType_List){// 列表
            NSIndexPath *mainIndexPath = [NSIndexPath indexPathForRow:index inSection:0];
            NSIndexPath *indexPath;
            if(fileVM.fileType == HYHomeFileType_Video){
                NSInteger videoIndex = [self.videoSource indexOfObject:fileVM];
                indexPath = [NSIndexPath indexPathForRow:videoIndex inSection:0];
            }else{
                NSInteger audioIndex = [self.videoSource indexOfObject:fileVM];
                indexPath = [NSIndexPath indexPathForRow:audioIndex inSection:0];
            }
            [self.reloadSubject sendNext:RACTuplePack(@(fileVM.fileType),mainIndexPath,indexPath)];
        }else{// 网格
            NSIndexPath *mainIndexPath = [NSIndexPath indexPathForItem:index inSection:0];
            NSIndexPath *indexPath;
            if(fileVM.fileType == HYHomeFileType_Video){
                NSInteger videoIndex = [self.videoSource indexOfObject:fileVM];
                indexPath = [NSIndexPath indexPathForItem:videoIndex inSection:0];
            }else{
                NSInteger audioIndex = [self.videoSource indexOfObject:fileVM];
                indexPath = [NSIndexPath indexPathForItem:audioIndex inSection:0];
            }
            [self.reloadSubject sendNext:RACTuplePack(@(fileVM.fileType),mainIndexPath,indexPath)];
        }
    }else{
        if([fileVM modifyFileNameToDB]){
            if(type == HYLayoutType_List){
                NSIndexPath *mainIndexPath = [NSIndexPath indexPathForRow:index inSection:0];
                [self.reloadSubject sendNext:RACTuplePack(@(fileVM.fileType),mainIndexPath)];
            }else{
                NSIndexPath *mainIndexPath = [NSIndexPath indexPathForItem:index inSection:0];
                [self.reloadSubject sendNext:RACTuplePack(@(fileVM.fileType),mainIndexPath)];
            }
        }else{
            [HYHub showMessage:NSLocalizedString(@"重命名失败", nil)];
        }
    }
}

#pragma mark - 选择移动到的文件夹
- (void)selectMoveToFolderItemViewModel:(HYHomeFileItemViewModel *)itemViewModel
{
    itemViewModel.isSelect = !itemViewModel.isSelect;
    if(self.selectFolderModel == itemViewModel){
        self.selectFolderModel = nil;
    }else{
        self.selectFolderModel.isSelect = NO;
        self.selectFolderModel = itemViewModel;
    }
}

- (void)moveToFolderWithCurrentFile:(HYHomeFileItemViewModel *)currentFileModel
                      completeBlock:(dispatch_block_t)completeBlock
{
    if(!self.selectFolderModel){
        [HYHub showMessage:NSLocalizedString(@"请选择移动至的‘文件夹’", nil)];
        return;
    }
    //1.更新数据库所在文件位置
    if(currentFileModel.fileType == HYHomeFileType_Folder){
        currentFileModel.folderLocation = self.selectFolderModel.folderIdentification;
    }else{
        currentFileModel.folderIdentification = self.selectFolderModel.folderIdentification;
    }
    BOOL isUpdate = [currentFileModel moveFileLocationToDB];
    if(isUpdate){
        if([self.selectFolderModel.fileName isEqualToString:NSLocalizedString(@"上一级", nil)] && !self.selectFolderModel.folderLocation.length){
            [self.refreshSubject sendNext:nil];
        }
        //2.更新数据库，刷新类标
        [self cancelSelectStatus];
        if(currentFileModel.fileType == HYHomeFileType_Folder){
            [self.folderSoucre removeObject:currentFileModel.folderModel];
        }else{
            [self.videoPlaySource removeObject:currentFileModel.videoModel];
            if(currentFileModel.fileType == HYHomeFileType_Video){
                [self.videoSource removeObject:currentFileModel];
            }else{
                [self.audioSource removeObject:currentFileModel];
            }
        }
        [self.dataSource removeObject:currentFileModel];
        [self.updatedContentSignal sendNext:self.dataSource];
        completeBlock();
    }
}

- (void)cancelSelectStatus
{
    if(self.selectFolderModel){
        self.selectFolderModel.isSelect = NO;
        self.selectFolderModel  = nil;
    }
}

#pragma mark - 移除文件或文件夹
- (void)deleteFilWithFileViewModel:(HYHomeFileItemViewModel *)fileVM
{
    if([fileVM deleteFileToDB]){
        if(fileVM.fileType == HYHomeFileType_Video){
            // 从播放源中移除视频model
            HYVideoModel *videoModel = [self fetchCurrentVideoWithFileModel:fileVM];
            [self.videoPlaySource removeObject:videoModel];
            [self.videoSource removeObject:fileVM];
        }else if (fileVM.fileType == HYHomeFileType_Audio){
            HYVideoModel *videoModel = [self fetchCurrentAudioWithFileModel:fileVM];
            [self.audioPlaySource removeObject:videoModel];
            [self.audioSource removeObject:fileVM];
        }
        [self.dataSource removeObject:fileVM];
        [self.updatedContentSignal sendNext:self.dataSource];
    }
}

#pragma mark - 加锁或解锁文件夹
- (void)lockOrUnLockFoldeWithFileViewModel:(HYHomeFileItemViewModel *)fileVM
{
    NSInteger mainIndex = [self.dataSource indexOfObject:fileVM];
    NSInteger index = 0;
    if([fileVM lockOrUnLockFolderToDB]){
        if(fileVM.fileType == HYHomeFileType_Video){
            index = [self.videoSource indexOfObject:fileVM];
            HYVideoModel *videoModel = [self fetchCurrentVideoWithFileModel:fileVM];
            videoModel.isPrivate = fileVM.isLock;
        }else if(fileVM.fileType == HYHomeFileType_Audio){
            index = [self.audioSource indexOfObject:fileVM];
            HYVideoModel *audioModel = [self fetchCurrentAudioWithFileModel:fileVM];
            audioModel.isPrivate = fileVM.isLock;
        }
        NSIndexPath *mainIndexPath = [NSIndexPath indexPathForRow:mainIndex inSection:0];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
        [self.reloadSubject sendNext:RACTuplePack(@(fileVM.fileType),mainIndexPath,indexPath)];
    }
}

#pragma mark - 新建视频
- (void)addNewVideoWithVideoModel:(HYHomeVideoModel *)videoModel
{
    // 从数据库中重新获取一次，是为了_id有值，防止updateToDB更新不成功
    NSString *sqlStr = [NSString stringWithFormat:@"SELECT *from VIDEOLIST WHERE folderIdentification = '%@' AND videoTitle = '%@'",videoModel.folderIdentification,videoModel.videoTitle];
    HYHomeVideoModel *sqVideoModel = [[HYHomeVideoModel searchWithSQL:sqlStr] firstObject];
    // 添加到首页列表数据源中
    HYHomeFileItemViewModel *fileModel = [[HYHomeFileItemViewModel alloc]initWitVideoModel:sqVideoModel];
    fileModel.setSubject = self.setSubject;
    if(self.videoPlaySource.count){
        [self.dataSource insertObject:fileModel atIndex:self.folderSoucre.count];
    }else{
        [self.dataSource addObject:fileModel];
    }
    if(sqVideoModel.isAudio){
        [self.audioSource insertObject:fileModel atIndex:0];
    }else{
        [self.videoSource insertObject:fileModel atIndex:0];
    }
    // 添加可播放视频列表中
    HYVideoModel *playModel = [[HYVideoModel alloc]init];
    playModel.videoTitle = videoModel.videoTitle;
    playModel.videoUrl = fileModel.filePath;
    playModel.createDate = fileModel.createDate;
    playModel.importDate = fileModel.importDate;
    playModel.videoDuration = fileModel.videoDuration;
    playModel.isAudio = sqVideoModel.isAudio;
    if(videoModel.isAudio){
        [self.audioPlaySource insertObject:playModel atIndex:0];
    }else{
        playModel.videoImage = [UIImage imageNamed:[HYFileManager videoImageSavePathWithFileName:videoModel.imageName]];
        playModel.videoImagePath = videoModel.imageName;
        [self.videoPlaySource insertObject:playModel atIndex:0];
    }
    [self.updatedContentSignal sendNext:self.dataSource];
}

#pragma mark - 刷新文件夹文件数量
- (void)reloadMyPhotoVideoCount:(NSInteger)videoCount
{
    HYHomeFolderModel *photoModel = self.folderSoucre[1];
    if(photoModel.fileCount != videoCount){
        photoModel.fileCount = videoCount;
        [photoModel updateToDB];
        HYHomeFileItemViewModel *fileModel = self.dataSource[1];
        fileModel.fileCount = videoCount;
        [self.reloadSubject sendNext:[NSIndexPath indexPathForRow:1 inSection:0]];
    }
}

- (BOOL)checkVideoPlayUrl:(NSString *)videoUrl
{
    //WMV,AVI,MKV,RMVB,RM,XVID,MP4,3GP,MPG,MOV,m3u8
    if([videoUrl containsString:@".wmv"] || [videoUrl containsString:@".avi"] || [videoUrl containsString:@".mkv"] || [videoUrl containsString:@".rmvb"] || [videoUrl containsString:@".rm"] || [videoUrl containsString:@".xvid"] || [videoUrl containsString:@".mp4"] || [videoUrl containsString:@".3gp"] || [videoUrl containsString:@".mpg"] || [videoUrl containsString:@".mov"] || [videoUrl containsString:@".m3u8"]){
        return YES;
    }
    return NO;
}


#pragma mark - lazyLoad
// 首页数据
- (NSMutableArray *)headerSource
{
    if(!_headerSource){
        _headerSource = [NSMutableArray array];
    }
    return _headerSource;
}

- (NSMutableArray *)dataSource
{
    if(!_dataSource){
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (NSMutableArray *)videoSource
{
    if(!_videoSource){
        _videoSource = [NSMutableArray array];
    }
    return _videoSource;
}

- (NSMutableArray *)audioSource
{
    if(!_audioSource){
        _audioSource = [NSMutableArray array];
    }
    return _audioSource;
}

// 可播放视频数据源
- (NSMutableArray *)videoPlaySource
{
    if(!_videoPlaySource){
        _videoPlaySource = [NSMutableArray array];
    }
    return _videoPlaySource;
}
// 可播放音频数据源
- (NSMutableArray *)audioPlaySource
{
    if(!_audioPlaySource){
        _audioPlaySource = [NSMutableArray array];
    }
    return _audioPlaySource;
}

// 文件数组
- (NSMutableArray *)folderSoucre
{
    if(!_folderSoucre){
        _folderSoucre = [NSMutableArray array];
    }
    return _folderSoucre;
}

@end
